package com.example.actionsignin.http;

import com.autonavi.ae.utils.NaviUtils;
import com.example.actionsignin.constant.Url;

import java.net.CookieManager;
import java.util.concurrent.TimeUnit;

import okhttp3.JavaNetCookieJar;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by 旭 on 2017/9/23/023.
 */

public class HttpUtil {
    private static ApiService SERVER;
    private static final long TIME_OUT = 10 * 1000;

    public static ApiService onSend()
    {
        if (SERVER == null)
        {
            CookieManager cookieManager = new CookieManager();
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.connectTimeout(TIME_OUT, TimeUnit.MILLISECONDS);
            builder.cookieJar(new JavaNetCookieJar(cookieManager));

            SERVER = new Retrofit.Builder()
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .baseUrl(Url.BASE_URL)
                    .client(builder.build())
                    .build()
                    .create(ApiService.class);
        }

        return SERVER;
    }
}
